package org.example.expansionpoint.beanFactoryPostProcessor;

import lombok.Data;
import org.springframework.beans.BeansException;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ScannedGenericBeanDefinition;
import org.springframework.stereotype.Component;

/**
 * @Description:
 */
@SpringBootApplication
public class TestBeanFactoryPostProcessor {
    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(TestBeanFactoryPostProcessor.class, args);
        Dog dog = context.getBean(Dog.class);
        System.out.println(dog.name);
        System.out.println(dog.age);
        context.close();
    }


    @Data
    @Component("dog")
    static class Dog {
        private String name = "旺财";
        private Integer age = 2;
    }

    @Component
    static class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {

        @Override
        public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
            ScannedGenericBeanDefinition dogDefinition = (ScannedGenericBeanDefinition) beanFactory.getBeanDefinition("dog");
            MutablePropertyValues properties = dogDefinition.getPropertyValues();
            // 修改 dog Bean 的 name 信息
            properties.add("name", "小黑");
        }
    }
}
